var http = require('http');
var querystring = require('querystring');

const fs = require("fs");

function send(path, data) {
    var basePath = "/api/private/v1/";
    var content = querystring.stringify(data);

    let pathArr = path.split('/');
    let file = '../dataSource/' + pathArr.join('') + '.js'

    var options = {
        method: 'get',
        hostname: "127.0.0.1",
        port: 8888,
        path: content ? basePath + path + '?' + content : basePath + path,
        headers: { Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2MDM1MTI5MTAsImV4cCI6MTYwMzU5OTMxMH0.OeVULBU_nko7D46_8XyPTy8vcUBSMRfG1_JTtVA7ON8' }
    };

    var req = http.request(options, res => {

        res.setEncoding('utf8');
        res.on("data", function (chunk) {
            console.log('请求成功，状态码:', res.statusCode);
            fs.writeFileSync(file, 'var obj=', (err) => { })
            fs.appendFileSync(file, chunk + ';', (err) => { })
            fs.appendFileSync(file, 'export default obj', (err) => { })

        });
        res.on('error', function (e) {
            console.log('请求错误: ' + e.message);
        });

    });
    req.on('error', function (e) {
        console.log('错误信息: ' + e);
    });
    req.end();
}


// 请求目录
var requestList = [
    { name: 'menus', data: undefined },
    {
        name: 'users', data: {
            query: "",
            pagenum: 1,
            pagesize: 5
        }
    },
    { name: 'roles', data: undefined },
    { name: "rights/tree", data: undefined },
    { name: "rights/list", data: undefined },
    {
        name: "goods", data: {
            query: "",
            pagenum: 1,
            pagesize: 10
        }
    },
    { name: "categories", data: undefined },
    {
        name: "categories", data: {
            type: 3,
            pagenum: 1,
            pagesize: 5
        }
    },
    { name: "orders", data:  {
        query: "",
        pagenum: 1,
        pagesize: 10
      } },
      { name: "reports/type/1", data: undefined },

]

var param = requestList[requestList.length - 1];

send(param.name, param.data);





